package me.zk.practice1.hw;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class HWBJ43 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] nums = Arrays.stream(scanner.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();


        int n = Integer.parseInt(scanner.nextLine());
        int index = -1;
        Map<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int v1 = nums[i];
            if (map.containsKey(v1)) {
                if (Math.abs(map.get(v1)-i) <= n) {
                    index = map.get(v1);
                    break;
                }
            }
            map.put(v1,i);
        }
        System.out.println(index);
    }
}
